/**
 * Это класс управления процессами Приложения.
 * @lends phonebook.controller.Controller
 */
phonebook.controller.Controller = Base.extend({

    adminView: null,
    bigModel: null,
    sidebar: null,
    addDialog: null,
    adder:null,
    /**
     * Конструктор класса
     * @author Konstantin Chumachenko
     * @augments Base
     * @class Класс управления процессами Приложения.
     * 
     * @constructs
     */
    constructor : function() {
        console.log("-- Controller: Конструктор4.");
        var me = this;
        me.bigModel = new phonebook.model.AutocompleteModel(me);
        me.bigModel.load();
        me.adminView = new phonebook.view.AdminView(me);
        me.sidebar = new phonebook.view.SidebarView(me);
        me.addDialog = new phonebook.view.AddNewDialog(me);
        me.adder = new phonebook.view.AdderView(me);
    },
    /**
     * Добавление в таблицу OrgUnitType
     */
    addOrgUnitType: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addOrgUnitType',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.orgUnitType.close();          
                me.refreshOrgUnitType();
                console.log("success");
            }
        });
    },
    /**
     * Добавление в таблицу OrgUnit
     */
    addOrgUnit: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addOrgUnit',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.orgUnit.close();          
                me.refreshOrgUnit();
                console.log("success");
            }
        });
    },
    /**
     * Добавление в таблицу Post
     */
    addPost: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addPost',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.post.close();          
                me.refreshPost();
                console.log("success");
            }
        });  
    },
    /**
     * Добавление в таблицу PhoneType
     */
    addPhoneType: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addPhoneType',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.phoneType.close();          
                me.refreshPhoneType();
                console.log("success");
            }
        });  
    },
    /**
     * Добавление в Phone
     */
    addPhone: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addPhone',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.phone.close();          
                me.refreshPhone();
                console.log("success");
            }
        });  
    },
    /**
     * Добавление в Room
     */
    addRoom: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addRoom',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.room.close();          
                me.refreshRoom();
                console.log("success");
            }
        });  
    },
    /**
     * Добавление в Person
     */
    addPerson: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addPerson',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.person.close();          
                me.refreshPerson();
                console.log("success");
            }
        });  
    },
    /**
     * Добавление в OrgUnitPost
     */
    addOrgUnitPost: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addOrgUnitPost',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){             
                me.addDialog.orgUnitPost.close();          
                me.refreshOrgUnitPost();
                console.log("success");
            }
        });  
    },
    addFromAdder: function(data){
        var me=this;
        $.ajax({
            type: 'POST',
            url: 'index?ajax=admin&query=addAll',
            data: data,
            async: false,
            success: function(data,  textStatus,  jqXHR){
                console.log("success");
                me.refreshAll();
            }
        });  
    },
    /**
     * Удаление из таблицы table по id
     * @param id
     * @param table
     */
    deleteById: function(id,table){
        var me=this;
        console.log(id);
        console.log(table);       
        $.ajax({
            type: 'POST',
            dataType: "json",
            url: 'index?ajax=admin&query=delete'+table,
            data: {
                "id": id
            },
            async: false,
            success: function(data,  textStatus,  jqXHR){
                if (data.error == undefined){
                    if (table == "OrgUnitType"){
                        me.refreshOrgUnitType();
                    }  
                    if (table == "OrgUnit"){                        
                        me.refreshOrgUnit();
                    }    
                    if (table == "Post"){
                        me.refreshPost();
                    } 
                    if (table == "PhoneType"){
                        me.refreshPhoneType();
                    }  
                    if (table == "Phone"){
                        me.refreshPhone();
                    }    
                    if (table == "Room"){
                        me.refreshRoom();
                    }
                    if (table == "Person"){
                        me.refreshPerson();
                    }
                    if (table == "OrgUnitPost"){
                        me.refreshOrgUnitPost();
                    }
                }else{
                    jAlert(data.error,"Ошибка");
                }
            }
        });
      
    },
    /**
     * Открытие диалого добавление в таблицу
     * @param table Таблица
     */
    showAddDialog: function(table){
        var me = this;
        if (table == "OrgUnitType"){
            me.addDialog.orgUnitType.render();
        }  
        if (table == "OrgUnit"){                        
            me.addDialog.orgUnit.render();
        }    
        if (table == "Post"){
            me.addDialog.post.render();
        } 
        if (table == "PhoneType"){
            me.addDialog.phoneType.render();
        }  
        if (table == "Phone"){
            me.addDialog.phone.render();
        }    
        if (table == "Room"){
            me.addDialog.room.render();
        }
        if (table == "Person"){
            me.addDialog.person.render();
        }
        if (table == "OrgUnitPost"){
            me.addDialog.orgUnitPost.render();
        }
    },
    refreshAll: function(){
        this.bigModel.load();  
        var model = this.bigModel.getModel();
        this.adminView.orgUnitTypeTable.fnClearTable();
        this.adminView.orgUnitTypeTable.fnAddData(model["orgUnitType"]);
        this.adminView.orgUnitTable.fnClearTable();
        this.adminView.orgUnitTable.fnAddData(model["orgUnit"]);
        this.adminView.postTable.fnClearTable();
        this.adminView.postTable.fnAddData(model["post"]);
        this.adminView.phoneTypeTable.fnClearTable();
        this.adminView.phoneTypeTable.fnAddData(model["phoneType"]);
        this.adminView.phoneTable.fnClearTable();
        this.adminView.phoneTable.fnAddData(model["phone"]);
        this.adminView.roomTable.fnClearTable();
        this.adminView.roomTable.fnAddData(model["room"]);
        this.adminView.personTable.fnClearTable();
        this.adminView.personTable.fnAddData(model["person"]);
        this.adminView.orgUnitPostTable.fnClearTable();
        this.adminView.orgUnitPostTable.fnAddData(model["orgUnitPost"]);
    },
    refreshOrgUnitType: function(){
        this.bigModel.load();  
        var model = this.bigModel.getModel()["orgUnitType"];
        this.adminView.orgUnitTypeTable.fnClearTable();
        this.adminView.orgUnitTypeTable.fnAddData(model);
    },
    refreshOrgUnit: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["orgUnit"];
        this.adminView.orgUnitTable.fnClearTable();
        this.adminView.orgUnitTable.fnAddData(model);
    },
    refreshPost: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["post"];
        this.adminView.postTable.fnClearTable();
        this.adminView.postTable.fnAddData(model);
    },
    refreshPhoneType: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["phoneType"];
        this.adminView.phoneTypeTable.fnClearTable();
        this.adminView.phoneTypeTable.fnAddData(model);
    },
    refreshPhone: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["phone"];
        this.adminView.phoneTable.fnClearTable();
        this.adminView.phoneTable.fnAddData(model);
    },
    refreshRoom: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["room"];
        this.adminView.roomTable.fnClearTable();
        this.adminView.roomTable.fnAddData(model);
    },
    refreshPerson: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["person"];
        this.adminView.personTable.fnClearTable();
        this.adminView.personTable.fnAddData(model);
    },
    refreshOrgUnitPost: function(){
        this.bigModel.load();
        var model = this.bigModel.getModel()["orgUnitPost"];
        this.adminView.orgUnitPostTable.fnClearTable();
        this.adminView.orgUnitPostTable.fnAddData(model);
    }
});
